var mysql = require('./mydb.js')
var request = require('request')
var schedule = require("node-schedule");
var fs = require('fs')

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36',
    'Referer': 'https://www.zhihu.com/',
    'Host': 'www.zhihu.com',
    'Upgrade-Insecure-Requests': '1',
    'Cookie':'q_c1=4b5fc21581e444318dd269127c421bcf|1515402388000|1515402388000; _zap=f9289d33-60ea-48f3-916e-d978ac85cdf6; __utmz=155987696.1516001728.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); capsion_ticket="2|1:0|10:1516003874|14:capsion_ticket|44:YmI4NjdmMWQ0NjQ4NDkxMzliYWE4NDZjNTc1YzM5OTQ=|516edcc97bbcced4301a59c301c1411b9275446db1e5e98c4f7e9b588db5161d"; z_c0="2|1:0|10:1516003882|4:z_c0|92:Mi4xeWpGRkJ3QUFBQUFBWUdMSjUtNzlEQ1lBQUFCZ0FsVk5LckJKV3dCZjFOR2NLc2xvWFF0YUFjYkQ0LWRRU0NzUXZn|2a6b86670a301a717da013f9fdb1d7329bd90547f3b684ac244792b680e74e44"; __utma=155987696.1308425753.1516001728.1516086422.1516089926.3; aliyungf_tc=AQAAAEomaWEFhwgAdTRM2iQ78lWiUR0q; d_c0="AADmchbP_wyPTjiA5HMKXPDYxcuQCTxB4L0=|1516105477"; _xsrf=35d718a4-de88-4330-b41a-cea937293e3b; anc_cap_id=832299c8e5e14d95a671efb9140ac0f6'
}

var namesize = function (url,name){
	var url1 = 'https://www.zhihu.com/api/v4/members/'+url+'?include=locations%2Cemployments%2Cgender%2Ceducations%2Cbusiness%2Cvoteup_count%2Cthanked_Count%2Cfollower_count%2Cfollowing_count%2Ccover_url%2Cfollowing_topic_count%2Cfollowing_question_count%2Cfollowing_favlists_count%2Cfollowing_columns_count%2Cavatar_hue%2Canswer_count%2Carticles_count%2Cpins_count%2Cquestion_count%2Ccolumns_count%2Ccommercial_question_count%2Cfavorite_count%2Cfavorited_count%2Clogs_count%2Cincluded_answers_count%2Cincluded_articles_count%2Cincluded_text%2Cmessage_thread_token%2Caccount_status%2Cis_active%2Cis_bind_phone%2Cis_force_renamed%2Cis_bind_sina%2Cis_privacy_protected%2Csina_weibo_url%2Csina_weibo_name%2Cshow_sina_weibo%2Cis_blocking%2Cis_blocked%2Cis_following%2Cis_followed%2Cis_org_createpin_white_user%2Cmutual_followees_count%2Cvote_to_count%2Cvote_from_count%2Cthank_to_count%2Cthank_from_count%2Cthanked_count%2Cdescription%2Chosted_live_count%2Cparticipated_live_count%2Callow_message%2Cindustry_category%2Corg_name%2Corg_homepage%2Cbadge%5B%3F(type%3Dbest_answerer)%5D.topics';
	request.head({url:url1,headers:headers}, function(err, res, body){
		if(err) throw err;
		request({url:url1,headers:headers}).pipe(fs.createWriteStream('./public/image/'+name+'.json'));
		console.log(name)
	});
}

var rule1 = new schedule.RecurrenceRule();
rule1.second  = [1,3,5,7,9,11,13,15,17,19];

mysql.query('select * from zhihuperson LIMIT 10 OFFSET 0',function(err, rows, fields){
	if(err) throw err;
	for(i in rows){
		var name = rows[i].id+rows[i].name;
		var url = rows[i].url_tokenadresstab;
		namesize(url,name);
	}
});